<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="home-page">
    <!-- 轮播图 -->
    <section class="hero-section">
      <CarouselBanner />
    </section>

    <!-- 主要内容 -->
    <div class="main-container">
      <!-- 推荐内容 -->
      <section class="recommend-section">
        <div class="section-header">
          <h2><el-icon><Star /></el-icon> 精选推荐</h2>
          <p>为您精心挑选的优质内容</p>
        </div>
        
        <div class="content-grid">
          <!-- 推荐文化 -->
          <div class="content-block">
            <div class="block-header">
              <h3>剪纸文化</h3>
              <router-link to="/culture" class="more-link">
                查看更多 <el-icon><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="content-list">
              <div 
                v-for="culture in recommendCultures" 
                :key="culture.id"
                class="content-card culture-card"
                @click="$router.push(`/culture/${culture.id}`)"
              >
                <div class="card-image">
                  <img 
                    :src="getFullImageUrl(culture.coverImage)" 
                    :alt="culture.title"
                    @error="(e) => handleImageError(e, 'culture')"
                    loading="lazy"
                  />
                </div>
                <div class="card-content">
                  <h4>{{ culture.title }}</h4>
                  <p>{{ culture.summary }}</p>
                  <div class="card-stats">
                    <span><el-icon><View /></el-icon> {{ culture.viewCount }}</span>
                    <span><el-icon><Star /></el-icon> {{ culture.likeCount }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 推荐教程 -->
          <div class="content-block">
            <div class="block-header">
              <h3>热门教程</h3>
              <router-link to="/tutorial" class="more-link">
                查看更多 <el-icon><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="content-list">
              <div 
                v-for="tutorial in recommendTutorials" 
                :key="tutorial.id"
                class="content-card tutorial-card"
                @click="$router.push(`/tutorial/${tutorial.id}`)"
              >
                <div class="card-image">
                  <img 
                    :src="getFullImageUrl(tutorial.coverImage)" 
                    :alt="tutorial.title"
                    @error="(e) => handleImageError(e, 'tutorial')"
                    loading="lazy"
                  />
                  <div class="difficulty-badge" :class="`level-${tutorial.difficultyLevel}`">
                    {{ getDifficultyText(tutorial.difficultyLevel) }}
                  </div>
                </div>
                <div class="card-content">
                  <h4>{{ tutorial.title }}</h4>
                  <p>{{ tutorial.summary }}</p>
                  <div class="card-stats">
                    <span><el-icon><User /></el-icon> {{ tutorial.studyCount }}</span>
                    <span><el-icon><Clock /></el-icon> {{ tutorial.estimatedTime }}min</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- 最新资讯 -->
      <section class="news-section">
        <div class="section-header">
          <h2><el-icon><Bell /></el-icon> 最新资讯</h2>
          <router-link to="/news" class="more-link">
            查看更多 <el-icon><ArrowRight /></el-icon>
          </router-link>
        </div>
        
        <div class="news-grid">
          <div 
            v-for="news in latestNews" 
            :key="news.id"
            class="news-card"
            @click="$router.push(`/news/${news.id}`)"
          >
            <div class="news-image">
              <img 
                :src="getFullImageUrl(news.coverImage)" 
                :alt="news.title"
                @error="(e) => handleImageError(e, 'news')"
                loading="lazy"
              />
              <div class="news-type" :class="`type-${news.type}`">
                {{ getNewsTypeText(news.type) }}
              </div>
            </div>
            <div class="news-content">
              <h4>{{ news.title }}</h4>
              <p>{{ news.summary }}</p>
              <div class="news-meta">
                <span class="news-date">{{ formatDate(news.createTime) }}</span>
                <span class="news-views">{{ news.viewCount }} 阅读</span>
              </div>
            </div>
          </div>
        </div>
      </section>

    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Star, ArrowRight, View, User, Clock, Bell
} from '@element-plus/icons-vue'
import { cultureApi, tutorialApi, newsApi } from '@/api'
import type { Culture, Tutorial, News } from '@/types/common'
import { TUTORIAL_DIFFICULTY, NEWS_TYPES } from '@/types/common'
import { getFullImageUrl, handleImageError } from '@/utils/imageUtils'
import CarouselBanner from '@/components/CarouselBanner.vue'

const router = useRouter()

// 响应式数据
const loading = ref(true)
const recommendCultures = ref<Culture[]>([])
const recommendTutorials = ref<Tutorial[]>([])
const latestNews = ref<News[]>([])

// 获取推荐文化内容
const fetchRecommendCultures = async () => {
  try {
    const response = await cultureApi.getRecommendCultures(6)
    recommendCultures.value = response.data
  } catch (error) {
    console.error('获取推荐文化内容失败:', error)
  }
}

// 获取推荐教程
const fetchRecommendTutorials = async () => {
  try {
    const response = await tutorialApi.getRecommendTutorials(6)
    recommendTutorials.value = response.data
  } catch (error) {
    console.error('获取推荐教程失败:', error)
  }
}

// 获取最新资讯
const fetchLatestNews = async () => {
  try {
    const response = await newsApi.getNewsPage({ page: 1, size: 6 })
    latestNews.value = response.data.list
  } catch (error) {
    console.error('获取最新资讯失败:', error)
  }
}


// 获取难度文本
const getDifficultyText = (level: number) => {
  return TUTORIAL_DIFFICULTY[level as keyof typeof TUTORIAL_DIFFICULTY] || '未知'
}

// 获取资讯类型文本
const getNewsTypeText = (type: number) => {
  return NEWS_TYPES[type as keyof typeof NEWS_TYPES] || '其他'
}

// 格式化日期
const formatDate = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    month: '2-digit',
    day: '2-digit'
  })
}

// 加载所有数据
const loadData = async () => {
  loading.value = true
  try {
    await Promise.all([
      fetchRecommendCultures(),
      fetchRecommendTutorials(),
      fetchLatestNews()
    ])
  } catch (error) {
    ElMessage.error('加载数据失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.home-page {
  min-height: 100vh;
}

/* 轮播图样式 */
.hero-section {
  margin-bottom: 40px;
  padding: 0 24px;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

@media (max-width: 768px) {
  .hero-section {
    padding: 0 16px;
    margin-bottom: 28px;
  }
}

@media (max-width: 480px) {
  .hero-section {
    padding: 0 12px;
    margin-bottom: 24px;
  }
}

/* 主容器 */
.main-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 区块通用样式 */
.section-header {
  text-align: center;
  margin-bottom: 40px;
}

.section-header h2 {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.section-header p {
  color: #666;
  font-size: 16px;
}

.more-link {
  color: #d32f2f;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 500;
}

.more-link:hover {
  text-decoration: underline;
}

/* 推荐内容样式 */
.recommend-section {
  margin-bottom: 60px;
}

.content-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
}

.content-block {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.block-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.block-header h3 {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.content-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.content-card {
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
  background: #f9f9f9;
}

.content-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.card-image {
  position: relative;
  width: 100%;
  height: 120px;
  overflow: hidden;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.content-card:hover .card-image img {
  transform: scale(1.05);
}

.difficulty-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
  color: #fff;
}

.difficulty-badge.level-1 {
  background: #4caf50;
}

.difficulty-badge.level-2 {
  background: #ff9800;
}

.difficulty-badge.level-3 {
  background: #f44336;
}

.card-content {
  padding: 12px;
}

.card-content h4 {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 4px;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-content p {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-stats {
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: #999;
}

.card-stats span {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* 资讯样式 */
.news-section {
  margin-bottom: 60px;
}

.news-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.news-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.news-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.news-image {
  position: relative;
  width: 100%;
  height: 180px;
  overflow: hidden;
}

.news-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.news-card:hover .news-image img {
  transform: scale(1.05);
}

.news-type {
  position: absolute;
  top: 12px;
  left: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  color: #fff;
}

.news-type.type-1 {
  background: #2196f3;
}

.news-type.type-2 {
  background: #4caf50;
}

.news-type.type-3 {
  background: #ff9800;
}

.news-content {
  padding: 20px;
}

.news-content h4 {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.news-content p {
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #999;
}


/* 响应式设计 */
@media (max-width: 768px) {
  .content-grid {
    grid-template-columns: 1fr;
    gap: 24px;
  }

  .content-list {
    grid-template-columns: 1fr;
  }

  .news-grid {
    grid-template-columns: 1fr;
  }

  .section-header h2 {
    font-size: 24px;
  }
}
</style>
